package org.zjsru.springclouddemo5418.Poke;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class PokerGameController {
    private Deck deck = new Deck();
    private Card playerCard;
    private Card computerCard;
    private String result;

    @RequestMapping("/")
    public String index() {
        deck.shuffle();
        return "index";
    }

    @GetMapping("/play")
    @ResponseBody
    public GameResult play() {
        playerCard = deck.drawCard();
        computerCard = deck.drawCard();

        int comparison = CardComparator.compare(playerCard, computerCard);
        if (comparison > 0) {
            result = "你赢了！";
        } else if (comparison < 0) {
            result = "你输了！";
        } else {
            result = "平局！";
        }

        return new GameResult(playerCard.toString(), computerCard.toString(), result);
    }

    @GetMapping("/reset")
    @ResponseBody
    public GameResult reset() {
        deck = new Deck();
        deck.shuffle();
        return new GameResult("", "", "游戏已重置！");
    }
}

class GameResult {
    private String playerCard;
    private String computerCard;
    private String result;

    public GameResult(String playerCard, String computerCard, String result) {
        this.playerCard = playerCard;
        this.computerCard = computerCard;
        this.result = result;
    }

    public String getPlayerCard() { return playerCard; }
    public String getComputerCard() { return computerCard; }
    public String getResult() { return result; }
}